package ch.uzh.ifi.seal.ssqa.popup.actions;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.TreeSelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IActionDelegate;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.progress.IProgressService;

import ch.uzh.ifi.seal.ssqa.importer.code.SourceCodeModelProvider;
import ch.uzh.ifi.seal.ssqa.model.DiagramComponent;
import ch.uzh.ifi.seal.ssqa.util.DiagramComponentProvider;

public class ImportSoftwareModelAction implements IObjectActionDelegate {

	Shell shell;

	/**
	 * Constructor for Action1.
	 */
	public ImportSoftwareModelAction() {
		super();
	}

	/**
	 * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
	 */
	public final void setActivePart(IAction action, IWorkbenchPart targetPart) {
		this.shell = targetPart.getSite().getShell();
	}

	/**
	 * starts the source code importer dependent on the selected file.
	 * 
	 * @see IActionDelegate#run(IAction)
	 */
	public void run(IAction action) {

		IRunnableWithProgress r = new IRunnableWithProgress() {

			@Override
			public void run(IProgressMonitor monitor)
					throws InvocationTargetException, InterruptedException {
				try {
					
					final IProgressMonitor monitorCopy = monitor;					
					final IWorkspace workspace = ResourcesPlugin.getWorkspace();
					IWorkspaceRoot workspaceRoot = ResourcesPlugin
							.getWorkspace().getRoot();

					final IWorkbench wb = PlatformUI.getWorkbench();
					wb.getDisplay().syncExec(new Runnable() {

						@Override
						public void run() {
							final IProgressMonitor monitor2 = monitorCopy;
							List<DiagramComponent> components = null;
							IWorkbenchWindow window = PlatformUI.getWorkbench()
									.getActiveWorkbenchWindow();
							TreeSelection selection = (TreeSelection) window
									.getSelectionService().getSelection();
							if (selection.size() == 1) {
								IFile f = (IFile) selection.getFirstElement();
								components = new DiagramComponentProvider(f)
										.getDiagramComponents();

							} else {
								/*
								 * ErrorDialog .openError( this.shell,
								 * "Resource not found",
								 * "The resource have selected can not be found or you have selected more than only one resource!"
								 * , null);
								 */
								System.out
										.println("Resource not found during importing software model action");
							}
							if (components != null) {
								SourceCodeModelProvider sourceCode = new SourceCodeModelProvider();
								sourceCode.createSourceCodeModel(components, monitor2);
								sourceCode.persistModel();
								System.out
										.println("Succeed in importing software model action");
							} else {
								System.out
										.println("Components  are null. Failed in importing software model action");
							}

						}
					});
				} finally {
					monitor.done();
				}
			}
		};
		IWorkbench wb = PlatformUI.getWorkbench();
		IProgressService ps = wb.getProgressService();
		try {
			ps.busyCursorWhile(r);
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

		// ************************************************************************************

		/*
		 * List<DiagramComponent> components = null; IWorkbenchWindow window =
		 * PlatformUI.getWorkbench() .getActiveWorkbenchWindow(); TreeSelection
		 * selection = (TreeSelection) window.getSelectionService()
		 * .getSelection(); if (selection.size() == 1) { IFile f = (IFile)
		 * selection.getFirstElement(); components = new
		 * DiagramComponentProvider(f).getDiagramComponents(); } else {
		 * ErrorDialog .openError( this.shell, "Resource not found",
		 * "The resource have selected can not be found or you have selected more than only one resource!"
		 * , null); }
		 * 
		 * if (components != null) { SourceCodeModelProvider sourceCode = new
		 * SourceCodeModelProvider();
		 * sourceCode.createSourceCodeModel(components);
		 * sourceCode.persistModel();
		 * MessageDialog.open(MessageDialog.INFORMATION, this.shell,
		 * "Model Loaded", "The Model has been loaded successfully.", SWT.None);
		 * } else { ErrorDialog.openError(this.shell, "No components found",
		 * "No components specified in the selected resource!", null); }
		 */
	}

	/**
	 * @see IActionDelegate#selectionChanged(IAction, ISelection)
	 */
	public void selectionChanged(IAction action, ISelection selection) {

	}

}
